#include <avr/io.h>
#include <string.h>
#include "display.h"
#include "Wait.h"


#define DISPLAY_GRANDE  1
#define DISPLAY_PICCOLO 0

#define DISPLAY_BIG_DIGIT_NUM  5
#define DISPLAY_SMALL_DIGIT_NUM  5

#define DISPLAY_SPENTO 10
#define DISPLAY_TRATTINO 11
#define DISPLAY_UNDERSCORE 12
#define DISPLAY_UPPERSCORE 13

#define SPI_IN_PORT  PINB
#define SPI_OUT_PORT PORTB
#define SPI_IN  PB5
#define SPI_CS1 PB6  //Ex SPI_OUT
#define SPI_CS2 PB4  //Ex SPI_CS
#define SPI_CLK PB7


static volatile unsigned char Display1[5]={5,4,3,2,1};
static volatile unsigned char Display2[5]={5,4,3,2,1};
static volatile unsigned char DisplaySegmentValues[16]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x00,0x40,0x08,0x01,0x00,0x00};


//*******************************************************************
//                           DisplayInit
//*******************************************************************
void DisplayInit(void){
 /*
   DisplaysOff();
   Display1[0]=1;
   Display1[1]=1;
   Display1[2]=1;
   Display2[0]=1;
   Display2[1]=1;
   Display2[2]=1;
*/


}


//*******************************************************************
//                           DisplayWriteValues
//*******************************************************************
void DisplayWriteValues(void){
    volatile unsigned char i;
    for (i=0;i<5;i++){
      
      Display_BIG_DigitWrite(i,Display1[i]);
	  Display_SMALL_DigitWrite(i,Display2[i]);
      Wait(2);
      DisplaysOff();
      WaitShort();
    }

    Wait(2);


}

//*******************************************************************
//                           DisplayBCDtoSEGMENTS
//*******************************************************************
unsigned char DisplayBCDtoSEGMENTS(unsigned char Value){
   return DisplaySegmentValues[Value];


}

//*******************************************************************
//                           Display_BIG_DigitWrite
//*******************************************************************
void Display_BIG_DigitWrite(unsigned char Digit,unsigned char DigitValue){

   //DISPLAY GRANDE
   
   DisplayBigOff();
   PORTD=DisplayBCDtoSEGMENTS(DigitValue); 
   PORTC&=~0x1F;
   PORTC|=DisplayDigitToAnode1(Digit);

}

//*******************************************************************
//                           Display_SMALL_DigitWrite
//*******************************************************************
void Display_SMALL_DigitWrite(unsigned char Digit,unsigned char DigitValue){
	
  //DISPLAY PICCOLO

  DisplaySmallOff();
  PORTA=DisplayBCDtoSEGMENTS(DigitValue);	 
  PORTG&=~0x1F;
  PORTG|=DisplayDigitToAnode2(Digit);



}
//*******************************************************************
//                           DisplayDigitToAnode1
//*******************************************************************
unsigned char DisplayDigitToAnode1(unsigned char Digit){

  return ((1<<Digit)&0x1F);

}

//*******************************************************************
//                           DisplayDigitToAnode2
//*******************************************************************
unsigned char DisplayDigitToAnode2(unsigned char Digit){

  return ((1<<Digit)&0x1F);

}

//*******************************************************************
//                           DisplaysOff
//*******************************************************************
void DisplaysOff(void){

   PORTC&=~0x1F;
   PORTG&=~0x1F;
   PORTA=0;
   PORTD=0;
}

//*******************************************************************
//                           DisplayBigOff
//*******************************************************************
void DisplayBigOff(void){

   PORTC&=~0x1F;
   PORTD=0;
}

//*******************************************************************
//                           DisplaySmallOff
//*******************************************************************
void DisplaySmallOff(void){
  
   PORTG&=~0x1F;
   PORTA=0;
   
}

//*******************************************************************
//                           WriteDisplayBuffer
//*******************************************************************
/*
void WriteDisplayBuffer(void){

    unsigned int DisplayData_DispGrande;
    unsigned int DisplayData_DispPiccolo;
    unsigned char ShiftValue,i;
    DisplayData_DispPiccolo=(unsigned int)(DisplayData&0x0FFF);
    DisplayData_DispGrande=(unsigned int)((DisplayData>>12)&0x0FFF);
    for (i=0;i<3;i++){
      ShiftValue=i<<2;
      Display2[i]=(unsigned char)(DisplayData_DispPiccolo>>ShiftValue)&0x0F;
      Display1[i]=(unsigned char)(DisplayData_DispGrande>>ShiftValue)&0x0F;
    }


}
*/

//*******************************************************************
//                           BigDisplayWriteValue
//*******************************************************************
void BigDisplayWriteValue(unsigned long Value){
	 static unsigned long PreviousValue;
	 volatile unsigned char DigitValue[5];
	 volatile unsigned long TempValue=Value;
	 unsigned char i;
	 if (Value!=PreviousValue){
		DigitValue[0]=TempValue%10;
		TempValue=TempValue/10;
		DigitValue[1]=TempValue%10;
		TempValue=TempValue/10;
		DigitValue[2]=TempValue%10;
		TempValue=TempValue/10;
		DigitValue[3]=TempValue%10;
		TempValue=TempValue/10;
		DigitValue[4]=TempValue%10;
		TempValue=TempValue/10;		
		
		 for (i=4;i>0;i--){
			if (DigitValue[i]== 0) DigitValue[i]=DISPLAY_SPENTO;
		 else break;
		 }	 
		 memcpy((unsigned char *)Display1,(unsigned char*)DigitValue,DISPLAY_BIG_DIGIT_NUM);
	 						
	 }
	 PreviousValue=Value;
	 

	 
	 
	 
}

//*******************************************************************
//                           SmallDisplayWriteValue
//*******************************************************************
void SmallDisplayWriteValue(unsigned long Value){
	 static unsigned long PreviousValue;
	 volatile unsigned char DigitValue[5];
	 volatile unsigned long TempValue=Value;
	 unsigned char i;
	 if (Value!=PreviousValue){
		DigitValue[0]=TempValue%10;
		TempValue=TempValue/10;
		DigitValue[1]=TempValue%10;
		TempValue=TempValue/10;
		DigitValue[2]=TempValue%10;
		TempValue=TempValue/10;
		DigitValue[3]=TempValue%10;
		TempValue=TempValue/10;
		DigitValue[4]=TempValue%10;
		TempValue=TempValue/10;								
	 }
	 PreviousValue=Value;
	 
	 for (i=4;i>0;i--){
	    if (DigitValue[i]== 0) DigitValue[i]=DISPLAY_SPENTO;
	 else break;
	 }	 
	 
	 memcpy((unsigned char*)Display2,(unsigned char*)DigitValue,DISPLAY_SMALL_DIGIT_NUM);
	 
	 
}